From 7ead6d662a2f9d8498af6650ea38418c64b52048 Mon Sep 17 00:00:00 2001
From: Vincent Chen <vincent.chen@sifive.com>
Date: Mon, 24 Jan 2022 02:42:02 -0800
Subject: [PATCH 3/8] board: sifive: Set LED's color to purple in the U-boot
 stage

Set LED's color to purple in the U-boot stage. Because there are still
some functions to be executed before board_early_init_f(), it means
the LED's is not changed to purple instantly when entering the U-boot
stage.
---
 board/sifive/unmatched/pwm.c       | 7 +++++++
 board/sifive/unmatched/unmatched.c | 6 ++++++
 configs/sifive_unmatched_defconfig | 1 +
 3 files changed, 14 insertions(+)

diff --git a/board/sifive/unmatched/pwm.c b/board/sifive/unmatched/pwm.c
index e1cc02310a..bd67672c22 100644
--- a/board/sifive/unmatched/pwm.c
+++ b/board/sifive/unmatched/pwm.c
@@ -36,6 +36,7 @@ void pwm_device_init(void)
 	struct pwm_sifive_regs *pwm0, *pwm1;
 	pwm0 = (struct pwm_sifive_regs *)PWM0_BASE;
 	pwm1 = (struct pwm_sifive_regs *)PWM1_BASE;
+#ifdef CONFIG_SPL_BUILD
 	writel(PWM_CMP_DISABLE_VAL, (void *)&pwm0->cmp0);
 	/* Set the 3-color PWM LEDs to yellow in SPL */
 	writel(PWM_CMP_ENABLE_VAL, (void *)&pwm0->cmp1);
@@ -54,4 +55,10 @@ void pwm_device_init(void)
 	writel(PWM_CMP_ENABLE_VAL, (void *)&pwm1->cmp2);
 	writel(PWM_CMP_ENABLE_VAL, (void *)&pwm1->cmp3);
 	writel(PWM_CFG_INIT, (void *)&pwm1->cfg);
+#else
+	/* Set the 3-color PWM LEDs to purple in U-boot */
+	writel(PWM_CMP_DISABLE_VAL, (void *)&pwm0->cmp1);
+	writel(PWM_CMP_ENABLE_VAL, (void *)&pwm0->cmp2);
+	writel(PWM_CMP_ENABLE_VAL, (void *)&pwm0->cmp3);
+#endif
 }
diff --git a/board/sifive/unmatched/unmatched.c b/board/sifive/unmatched/unmatched.c
index 6295deeae2..30c082d001 100644
--- a/board/sifive/unmatched/unmatched.c
+++ b/board/sifive/unmatched/unmatched.c
@@ -22,6 +22,12 @@ void *board_fdt_blob_setup(int *err)
 	return (ulong *)&_end;
 }
 
+int board_early_init_f(void)
+{
+	pwm_device_init();
+	return 0;
+}
+
 int board_init(void)
 {
 	/* enable all cache ways */
diff --git a/configs/sifive_unmatched_defconfig b/configs/sifive_unmatched_defconfig
index d400ed0b23..0758f8e90f 100644
--- a/configs/sifive_unmatched_defconfig
+++ b/configs/sifive_unmatched_defconfig
@@ -51,3 +51,4 @@ CONFIG_DM_SCSI=y
 CONFIG_USB=y
 CONFIG_USB_XHCI_HCD=y
 CONFIG_USB_XHCI_PCI=y
+CONFIG_BOARD_EARLY_INIT_F=y
-- 
2.27.0

